<?php
define('APP_DIR', realpath('./'));
define('APP_EVN', 'nginx');
define('APP_SYS', 'windows');
$GLOBALS = require(APP_DIR . '/config.php');
require(APP_DIR . '/lib/core.php');
$app = new Core();
$app->request = array_merge($_GET, $_POST);
$app->server = $_SERVER;
$app->header = apache_request_headers();
if (!empty($GLOBALS['rewrite'])) {
    foreach ($GLOBALS['rewrite'] as $rule => $mapper) {
        if ('/' == $rule) $rule = '/$';
        if (0 !== stripos($rule, $app->server['REQUEST_SCHEME'])) $rule = $app->server['REQUEST_SCHEME'] . $app->server['HTTP_HOST'] . '/' . $rule;
        $rule = '/' . str_ireplace(array('\\\\', $app->server['REQUEST_SCHEME'], '/', '<', '>', '.'), array('', '', '\/', '(?P<', '>[-\w]+)', '\.'), $rule) . '/i';
        if (preg_match($rule, $app->server['REQUEST_SCHEME'] . $app->server['HTTP_HOST'] . $app->server['REQUEST_URI'], $matches)) {
            $route = explode("/", $mapper);if (isset($route[2])) {list($app->request['m'], $app->request['c'], $app->request['a']) = $route;} else {list($app->request['c'], $app->request['a']) = $route;}
            foreach ($matches as $matchkey => $matchval) {if (!is_int($matchkey)) $app->request[$matchkey] = $matchval;}break;
        }
    }
}
unset($app->request['s']);
$app->app_module = isset($app->request['m']) ? strtolower($app->request['m']) : 'home';unset($app->request['m']);
$app->app_controller = isset($app->request['c']) ? strtolower($app->request['c']) : 'main';unset($app->request['c']);
$app->app_action = isset($app->request['a']) ? strtolower($app->request['a']) : 'index';unset($app->request['a']);
header("Server: FoxBoot/v1.0");
$controller_name = str_replace("/", "\\", str_replace("//", "/", str_replace("\\\\", "\\", "app" . DS . $app->app_module . DS . 'controller' . DS . $app->app_controller . 'Control')));
if (!empty($app->app_module)) if (!is_available_className($app->app_module)) return err("Err: Module '$app->app_module' is not correct!");
if (!is_available_className($app->app_controller)) return err("Err: Controller '$controller_name' is not correct!");
if (!class_exists($controller_name, true)) return err("Err: Controller '$controller_name' is not exists!");
if (!method_exists($controller_name, $app->app_action)) return err("Err: Method '$app->app_action' of '$controller_name' is not exists!");
$controller_obj = new $controller_name($app);
$arr = explode('/', $app->server['REQUEST_URI']);
call_user_func_array(array($controller_obj, $app->app_action), count($arr) > 4 ? array_slice($arr, 4) : array());
if ($controller_obj->_auto_display) {
    $auto_tpl_name = (empty($app->app_module) ? '' : $app->app_module . DS) . $app->app_controller . '_' . $app->app_action . '.html';
    if (file_exists(APP_DIR . DS . 'app' . DS . 'view' . DS . $auto_tpl_name)) $controller_obj->display($auto_tpl_name);
}